package com.mijie.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.GZIPOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.xerial.snappy.Snappy;


public class IOUtil{

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(IOUtil.class);
	
	public static byte[] writeObject(Object object,boolean compress) throws IOException{
		ByteArrayOutputStream baos = null;
		GZIPOutputStream gzip = null;
		ObjectOutputStream oos = null;
		try{
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			oos.flush();
			if(compress){
				return Snappy.compress(baos.toByteArray());
			}else{
				return baos.toByteArray();
			}
		}finally{
			if(oos!=null) oos.close();
			if(gzip!=null) gzip.close();
			if(baos!=null) baos.close();
		}
	}
	
	
	public static Object readObject(byte[] src,boolean compress) throws IOException, ClassNotFoundException{
		ByteArrayInputStream bais = null;
		ObjectInputStream ois = null;
		try{
			if(compress){
				bais = new ByteArrayInputStream(Snappy.uncompress(src));
			}else{
				bais = new ByteArrayInputStream(src);
			}
			
			ois = new ObjectInputStream(bais);
			return ois.readObject();
		}finally{
			if(ois!=null) ois.close();
			if(bais!=null) bais.close();
		}
	}
	
	public static String getContent4InputStream(InputStream is,String charset) throws IOException{
		InputStreamReader isr = null;
		BufferedReader br = null;
		String line = null;
		StringBuilder sb = new StringBuilder();
		try{
			isr = new InputStreamReader(is,charset);
			br = new BufferedReader(isr);
			while((line=br.readLine())!=null){
				sb.append(line);
			}
		}finally{
			if(br!=null) br.close();
			if(isr!=null) isr.close();
			if(is!=null) is.close();
		}
		return sb.toString();
	}
	
	public static String getContent4InputStream(InputStream is) throws IOException{
		InputStreamReader isr = null;
		BufferedReader br = null;
		String line = null;
		StringBuilder sb = new StringBuilder();
		try{
			isr = new InputStreamReader(is,"UTF-8");
			br = new BufferedReader(isr);
			while((line=br.readLine())!=null){
				sb.append(line);
			}
		}finally{
			if(br!=null) br.close();
			if(isr!=null) isr.close();
			if(is!=null) is.close();
		}
		return sb.toString();
	}
	
	public static List<String> getList4InputStream(InputStream is) throws IOException{
		InputStreamReader isr = null;
		BufferedReader br = null;
		String line = null;
		List<String> list = new ArrayList<String>();
		try{
			isr = new InputStreamReader(is);
			br = new BufferedReader(isr);
			while((line=br.readLine())!=null){
				list.add(line);
			}
		}finally{
			if(br!=null) br.close();
			if(isr!=null) isr.close();
			if(is!=null) is.close();
		}
		return list;
	}
	
	public static boolean copyFileTo(File src,File dest){
		if(!src.exists()) return false;
		try {
			FileUtils.copyFile(src, dest);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			return false;
		}
        return true;
	}
	
	public static void write(HttpServletResponse response,String msg) throws IOException{
		Writer writer = response.getWriter();
		writer.write(msg);
		writer.close();
	}
}
